#include <iostream>
#include <fstream>
using namespace std;
#include "contacts.pb.h"
void AddPeopleInfo(contacts2::PeopleInfo* people)
{
    cout<<"-------------新增联系⼈-------------"<<endl;
    cout<<"请输⼊联系⼈姓名: ";
    string name;
    getline(cin,name);
    people->set_name(name);
    cout<<"请输⼊联系⼈年龄: ";
    int age;
    cin>>age;
    people->set_age(age);
    cin.ignore(256,'\n');//清空缓冲区的\n
    
    for(int i = 0;;i++)
    {
        cout<<"请输⼊联系⼈电话"<<i+1<<": ";
        string number;
        getline(cin,number);
        if(number=="")
            break; 
        contacts2::PeopleInfo_Phone* phone = people->add_phone();
        phone->set_number(number);
        cout<<"请输入电话类型(1.移动电话 2.固定电话): ";
        int type;
        cin>>type;
        cin.ignore(256,'\n');
        switch(type)
        {
            case 1:
                phone->set_type(::contacts2::PeopleInfo_Phone_PhoneType_MP);
                break;
            case 2:
                phone->set_type(::contacts2::PeopleInfo_Phone_PhoneType_TEL);
                break;
            default:
                cout<<"输入错误"<<endl;
                break;
        }

    }
    contacts2::Address address;
    string home_address;
    string unit_address;
    cout<<"请输入家庭地址: ";
    getline(cin,home_address);
    cout<<"请输入单位地址: ";
    getline(cin,unit_address);
    address.set_home_address(home_address);
    address.set_unit_address(unit_address);
    //将Address类型转换为Any类型
    people->mutable_data()->PackFrom(address);

    int other_contact;
    cout<<"请选择其他联系方式(1.qq  2.微信): ";
    cin>>other_contact;
    cin.ignore(256,'\n');
    if(other_contact==1)
    {
        cout<<"请输入qq: ";
        string qq;
        getline(cin,qq);
        people->set_qq(qq);
    }
    else if(other_contact==2)
    {
        cout<<"请输入微信: ";
        string wechat;
        getline(cin,wechat);
        people->set_wechat(wechat);
    }
    else
    {
        cout<<"选择错误,设置其他联系方式失败"<<endl;
    }

    for(int i = 0;;i++)
    {
        cout<<"请输入备注"<<i+1<<"标签(只输入回车即添加完成): ";
        string remark_key;
        getline(cin,remark_key);
        if(remark_key.empty())
            break;
        string remark_val;
        cout<<"请输入标签"<<i+1<<"内容: ";
        getline(cin,remark_val);
        people->mutable_remark()->insert({remark_key,remark_val});
    }
    cout<<"-----------添加联系⼈成功-----------"<<endl;
}
int main()
{
    contacts2::Contacts contacts;
    //打开文件，读取通讯录的信息
    fstream input("contacts.bin",ios::in | ios::binary);//因为文件中存储的是二进制的信息
    if(!input)
    {
        cout<<"contacts.bin not find , create a new file"<<endl;
    }
    else if(!contacts.ParseFromIstream(&input))
    {
        cerr<<"parse error"<<endl;
        input.close();
        return -1;
    }
    //添加信息
    AddPeopleInfo(contacts.add_people());
    //将信息写入文件
    fstream output("contacts.bin",ios::out | ios::trunc |ios::binary);//覆盖写因为上面已经将通讯录中的信息读出来了。
    if(!contacts.SerializeToOstream(&output))
    {
        cerr<<"write error"<<endl;
        output.close();
        input.close();
        return -2;
    }
    cout<<"write success"<<endl;
    output.close();
    input.close();
    return 0;
}